﻿using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using UnityEngine;

public class UnityTools
{
    public static System.Random mRandom = new System.Random();
    private static bool mCancelShake = false;


    public static void SetTransformRelative(Transform tf, float x = 0f, float y = 0f, float z = 0f, Space space = Space.Self)
    {
        if (space == Space.Self)
            tf.transform.localPosition = new Vector3(tf.transform.localPosition.x + x, tf.transform.localPosition.y + y, tf.transform.localPosition.z + z);
        else
            tf.transform.position = new Vector3(tf.transform.position.x + x, tf.transform.position.y + y, tf.transform.position.z + z);
    }
    public static void SetTransformAbsolute(Transform tf, float x = 0f, float y = 0f, float z = 0f, Space space = Space.Self)
    {
        if (space == Space.Self)
            tf.transform.localPosition = new Vector3(x, y, z);
        else
            tf.transform.position = new Vector3(x, y, z);
    }
    public static Vector3 GetTransformRelative(Transform tf, float x = 0f, float y = 0f, float z = 0f, Space space = Space.Self)
    {
        if (space == Space.Self)
            return new Vector3(tf.transform.localPosition.x + x, tf.transform.localPosition.y + y, tf.transform.localPosition.z + z);
        else
            return new Vector3(tf.transform.position.x + x, tf.transform.position.y + y, tf.transform.position.z + z);
    }

    public static void NewFindChild(Transform tf, float x = 0f, float y = 0f, float z = 0f, Space space = Space.Self)
    {

    }

    public static void SetActive(GameObject go, bool display)
    {
        if (go.activeSelf != display)
            go.SetActive(display);
    }

    public static Vector3 ToV3(Rect r)
    {
        Vector3 v3 = new Vector3(r.x + r.width / 2, r.y + r.height / 2, 0);
        return v3;
    }

    public static Vector2 ToV2(Rect r)
    {
        Vector2 v2 = new Vector3(r.x + r.width / 2, r.y + r.height / 2);
        return v2;
    }


    public static Vector3[] GetVector3Ary(Transform[] tf, Space space = Space.Self)
    {
        Vector3[] v3 = new Vector3[tf.Length];
        for (int i = 0; i < tf.Length; i++)
        {
            if (space == Space.World)
                v3[i] = tf[i].position;
            else
                v3[i] = tf[i].localPosition;
        }
        return v3;
    }



    /// <summary>
    /// x分之一的机会, 命中true , 否则是 false
    /// </summary>
    /// <param name="ratio"></param>
    /// <returns></returns>
    public static bool RangeHit(int ratio)
    {
        if (ratio == 0)
            return false;
        if (ratio == 1)
            return true;

        int result = mRandom.Next(1, ratio);
        return result == 1;
    }

    //public void CheckChildMaterialCount() 
    //{
    //    GameObject go = Selection.activeGameObject;
    //    if (go == null)
    //        return;

    //    MeshRenderer[] m = go.GetComponentsInChildren<MeshRenderer>();
    //    //m[0].material
    //    Material[] ml = null;
    //    List<string> strList = new List<string>();
    //    for (int i = 0; i < m.Length; i++)
    //    {
    //        ml = m[i].materials;
    //        for (int j = 0; j < ml.Length; j++)
    //        {
    //            bool isFull = true;
    //            for (int z = 0; z < strList.Count; z++)
    //            {
    //                if (strList[z] == ml[j].name)
    //                {
    //                    isFull = false;
    //                    break;
    //                }
    //            }

    //            if (isFull)
    //                strList.Add(ml[j].name);

    //        }
    //    }

    //    StringBuilder sb = new StringBuilder();
    //    foreach (var item in strList)
    //    {
    //        sb.AppendLine(item);
    //    }

    //    Debug.Log(strList.Count + "\r\n" + sb.ToString());
    //}


    public int GetDistance(int a, int b)
    {
        int result = 0;
        if ((a < 0 && b > 0) || (a > 0 && b < 0))
        {
            result = b + a;
        }
        else
        {
            result = Mathf.Abs(a - b);
        }

        return 0;
    }

    public static float GetDistance(float a, float b)
    {
        float result = 0;
        if ((a < 0 && b > 0) || (a > 0 && b < 0))
        {
            result = b + a;
        }
        else
        {
            result = Mathf.Abs(a - b);
        }

        return 0;
    }


    public static IEnumerator ShakeCamera(Transform transform, float shakeStrength = 0.1f, float rate = 8, float shakeTime = 0.3f)
    {
        float t = 0;
        float speed = 1 / shakeTime;
        Vector3 orgPosition = transform.localPosition;
        while (t < 1 && !mCancelShake)
        {
            t += Time.deltaTime * speed;
            transform.position = orgPosition + new Vector3(Mathf.Sin(rate * t), Mathf.Cos(rate * t), 0) * Mathf.Lerp(shakeStrength, 0, t);
            yield return null;
        }
        mCancelShake = false;
        transform.position = orgPosition;
    }


    [Conditional("PL_Debug"), Conditional("UNITY_EDITOR")]
    public static void BeginSample(string s)
    {
        //if(Profiler.enabled)
        //Profiler.BeginSample(s);
    }

    [Conditional("PL_Debug"), Conditional("UNITY_EDITOR")]
    public static void EndSample()
    {
        //if (Profiler.enabled)
        //Profiler.EndSample();
    }

    public enum Space
    {
        Self,
        World,
    }

}


public static class UnityStaticMethod
{
    /// <summary>
    /// if GameObject active. not do active.
    /// </summary>
    /// <param name="go"></param>
    /// <param name="display"></param>
    /// <param name="activeSelf"></param>
    public static void TryActive(this GameObject go, bool display, bool activeSelf = true)
    {
        if (activeSelf)
        {
            if (go.activeSelf != display)
                go.SetActive(display);
        }
        else
        {
            if (go.activeInHierarchy != display)
                go.SetActive(display);
        }
    }

    public static Transform FindEnd(this Transform go, string name)
    {
        var oList = go.GetComponentsInChildren(typeof(Transform), true);

        foreach (var node in oList)
        {
            if (node.name == name)
            {
                return node.transform;
            }
        }

        return null;
    }


}
